package com.stylefeng.guns.modular.ActionLog.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.modular.system.warpper.LogWarpper;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.ActionLog;
import com.stylefeng.guns.modular.ActionLog.service.IActionLogService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通行日志控制器
 *
 * @author fengshuonan
 * @Date 2018-08-24 10:39:57
 */
@Controller
@RequestMapping("/actionLog")
public class ActionLogController extends BaseController {

    private String PREFIX = "/ActionLog/actionLog/";

    @Autowired
    private IActionLogService actionLogService;

    /**
     * 跳转到通行日志首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "actionLog.html";
    }

    /**
     * 跳转到添加通行日志
     */
    @RequestMapping("/actionLog_add")
    public String actionLogAdd() {
        return PREFIX + "actionLog_add.html";
    }

    /**
     * 跳转到修改通行日志
     */
    @RequestMapping("/actionLog_update/{actionLogId}")
    public String actionLogUpdate(@PathVariable Integer actionLogId, Model model) {
        ActionLog actionLog = actionLogService.selectById(actionLogId);
        model.addAttribute("item",actionLog);
        LogObjectHolder.me().set(actionLog);
        return PREFIX + "actionLog_edit.html";
    }
    /**
     * 跳转到通行日志详情
     */
    @RequestMapping("/actionLog_detail/{actionLogId}")
    public String actionLogMessage(@PathVariable Integer actionLogId, Model model) {
        ActionLog actionLog = actionLogService.selectById(actionLogId);
        model.addAttribute("item",actionLog);
        LogObjectHolder.me().set(actionLog);
        return PREFIX + "actionLog_detail.html";
    }

    /**
     * 获取通行日志列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String carNumber, @RequestParam(required = false) String logType) {
        Page<ActionLog> page = new PageFactory<ActionLog>().defaultPage();
        List<Map<String, Object>> result = actionLogService.getActionLog(page, beginTime, endTime, carNumber, logType);
        page.setRecords((List<ActionLog>) new LogWarpper(result).warp());
        return super.packForBT(page);
    }


    /**
     * 新增通行日志
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ActionLog actionLog) {
        actionLogService.insert(actionLog);
        return SUCCESS_TIP;
    }

    /**
     * 删除通行日志
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer actionLogId) {
        actionLogService.deleteById(actionLogId);
        return SUCCESS_TIP;
    }

    /**
     * 修改通行日志
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ActionLog actionLog) {
        actionLogService.updateById(actionLog);
        return SUCCESS_TIP;
    }

    /**
     * 通行日志详情
     */
    @RequestMapping(value = "/detail/{actionLogId}")
    @ResponseBody
    public Object detail(@PathVariable("actionLogId") Integer actionLogId) {
        return actionLogService.selectById(actionLogId);
    }

    /**
     * 清空日志
     */
    @RequestMapping("/delLog")
    @ResponseBody
    public Object delLog() {
        SqlRunner.db().delete("delete from car_action_log");
        return SUCCESS_TIP;
    }

    /**
     * 测试ajax
     */
    @RequestMapping(value = "/testParm")
    @ResponseBody
    public void ajax(@RequestParam String carNumber,HttpServletResponse response) throws IOException {
        System.out.println("AAAABBBBB:"+carNumber);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        EntityWrapper<ActionLog> wrapper =new EntityWrapper<>();
        Wrapper<ActionLog> wrapper1=wrapper.like("car_number",carNumber);
        List<ActionLog> actionLogs=actionLogService.selectList(wrapper1);
        String fileName = "lcAAAA"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "编号", "车牌号", "通行时间", "通道号","方向","日志类型","备注"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (ActionLog actionLog : actionLogs) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(actionLog.getId());
            row1.createCell(1).setCellValue(actionLog.getCarNumber());
            row1.createCell(2).setCellValue(actionLog.getIoTime());
            row1.createCell(3).setCellValue(actionLog.getChannelId());
            row1.createCell(4).setCellValue(actionLog.getDirection());
            row1.createCell(5).setCellValue(actionLog.getLogType());
            row1.createCell(6).setCellValue(actionLog.getRemark());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }

    /**
     * 打印日志
     */
    @RequestMapping("/download")
    @ResponseBody
    public void download(HttpServletResponse response) throws IOException {
        System.out.println(response);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");

        List<ActionLog> classmateList = actionLogService.selectList(null);

        String fileName = "lcAAA"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "编号", "车牌号", "通行时间", "通道号","方向","日志类型","备注"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (ActionLog actionLog : classmateList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(actionLog.getId());
            row1.createCell(1).setCellValue(actionLog.getCarNumber());
            row1.createCell(2).setCellValue(actionLog.getIoTime());
            row1.createCell(3).setCellValue(actionLog.getChannelId());
            row1.createCell(4).setCellValue(actionLog.getDirection());
            row1.createCell(5).setCellValue(actionLog.getLogType());
            row1.createCell(6).setCellValue(actionLog.getRemark());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
}
}
